﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using eCommerce.Core;
using eCommerce.Core.Domain.Messges;
using eCommerce.Core.Helper;
using eCommerce.Services.Messages;
using eCommerce.Services.Products;
using eCommerce.Services.ShoppingCarts;

namespace eCommerce.Controllers.api
{
    public class AddToShoppingCartResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
    }
    public class AddToCartProductModel
    {
        public int ItemId{ get; set; }
        public int Quantity { get; set; }
    }

    public class ShoppingCartApiController : ApiController
    {
        private IProductService _productService;
        private IMessageService _messageService;
        private IShoppingCartService _shoppingCartService;
        private IWorkContext _workContext;
        public ShoppingCartApiController(IProductService productService, IMessageService messageService, IShoppingCartService shoppingCartService, IWorkContext workContext)
        {
            _productService = productService;
            _messageService = messageService;
            _shoppingCartService = shoppingCartService;
            _workContext = workContext;
        }

        public RemoveItemFromShoppingCartResult RemoveFromCart(RemoveItemFromShoppingCartRequest request)
        {
            var result = new RemoveItemFromShoppingCartResult();
            try
            {
                _shoppingCartService.RemoveShoppingCartItem(_workContext.CurrentCustomer.Id, request.ShoppingCarItemId);
                result.Status = "0";
            }
            catch (Exception e)
            {
                result.Status = "1";
                result.Message = e.Message;
            }
            return result;
        }

       
        public AddToShoppingCartResult AddCart(AddToCartProductModel addToCartProductModel)
        {
            var result = new AddToShoppingCartResult();

            var item = _productService.GetItemById(addToCartProductModel.ItemId);
            if (item == null)
            {
                result.Status = "1";
                result.Message = _messageService.GetResourceValue(MessageName.ItemNotExist,0);
                return result;
            }

            //quantity
            var quantity = 1;

            var attributes = item.ToDescription();

            var addToCartWarnings = new List<string>();

            addToCartWarnings.AddRange(_shoppingCartService.AddToCart(_workContext.CurrentCustomer.Id, item, attributes, quantity));

            if (addToCartWarnings.Count > 0)
            {

                result.Status = "1";
                result.Message = string.Join(",", addToCartWarnings);
                return result;
            }

           

            result.Status = "0";
            return result;

        }
    }

    public class RemoveItemFromShoppingCartRequest
    {
        public int ShoppingCarItemId { get; set; }
    }

    public class RemoveItemFromShoppingCartResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
    }
}
